import {ConfigEnv, loadEnv, UserConfig} from 'vite'

import createVuePlugin from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import svgLoader from 'vite-svg-loader'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'

import path from 'path'

const CWD = process.cwd()


export default ({mode}: ConfigEnv): UserConfig => {
    return {
        base: '/web/',
        resolve: {alias: {'@': path.resolve(__dirname, './src')}},
        css: {
            preprocessorOptions: {
                less: {
                    javascriptEnabled: true,
                    modifyVars: {
                        hack: `fas; @import (reference) "${path.resolve(__dirname, 'src/assets/style/variables.less')}";`,
                    },
                },
            },
        },


        plugins: [createVuePlugin({script: {defineModel: true}}), vueJsx(), svgLoader(), VueSetupExtend()],
        server: {
            port: 9200,
            host: '0.0.0.0',
            proxy: {
                '/api': {
                    target: 'http://127.0.0.1:9300/api',
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/, '')
                }
            },

        },

        build: {
            reportCompressedSize: false,
            // 大文件报警阈值设置,不建议使用
            chunkSizeWarningLimit: 1000,
            rollupOptions: {
                output: {
                    // 静态资源分类打包
                    chunkFileNames: 'static/js/[name]-[hash].js',
                    entryFileNames: 'static/js/[name]-[hash].js',
                    assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
                    manualChunks(id) {
                        if (id.includes('node_modules')) {
                            return id.toString().split('node_modules/')[1].split('/')[0].toString()
                        }
                    },
                },
            },
        },
    }
}
